package com.doublefish.doublefishrobot.utils;

import android.content.Context;

import com.doublefish.doublefishrobot.ui.home.bean.DeviceBean;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;

import java.util.List;

/**
 * 作者:dugaofeng on 2024/10/16 11:01
 */
public class AppCacheUtil {

    private Context appContext;

    private boolean didReadPrivacy = false;  ///是否阅读了隐私政策
    private boolean storagePermissionEnable = false; // //是否有存储权限 数据库是否可用 是否可以下载视频

    //是否下载高清视频 0.不下载（默认） 1.仅WiFi 2.WiFi和移动网络
    private int videoDownloadType= 0;

    //0X00代表左手 0X01代表右手 默认右手
    private int handTag;

    private int bgProgram = 0;// 超5的后台运行方案 默认0
    private float bgLight = 0.2f;// 超5的后台运行方案 亮度
    private boolean bgPlayer = true;// 超5的后台运行方案 播放MP3

    private int s3Ors5Type;   //记录当前连接的是超3还是超5 方便自定义的时候保存

    private boolean ignoreSpeedTips; //设置是否在速度较快时  进行提示

    //region  与遥控器同步的两条脚本
    private int remoteModelCount = 2;  //默认是2 写死
    private  int remoteModelDir = 9;   //文件夹 默认是9 写死
    //是否已将app01 app02 同步到发球机
    private boolean isSynchronizedForRemote;
    private ModeSettingBean s6Bean0;  //超6
    private ModeSettingBean s6Bean1;
    private ModeSettingBean s8Bean0;  //超8
    private ModeSettingBean s8Bean1;

    //app显示的名字
    private final String REMOTE_APP_0 = "01";
    private final String REMOTE_APP_1 = "02";

    //脚本名字简单点，方便蓝牙传输。 必须与iOS和发球机一致
    private final String REMOTE_SCRIPT_0 = "s1";
    private final String REMOTE_SCRIPT_1 = "s2";

    //script key
    private final String S8_SCRIPT_KEY_0 = "s8_remote_script_0";
    private final String S8_SCRIPT_KEY_1 = "s8_remote_script_1";
    private final String S6_SCRIPT_KEY_0 = "s6_remote_script_0";
    private final String S6_SCRIPT_KEY_1 = "s6_remote_script_1";

    //script
    private final String S8_SCRIPT_0 = "0,1,#0,40,#0,17,#0,34,#0,38,#0,6,#0,90,#\n";
    private final String S8_SCRIPT_1 = "0,1,#0,40,#0,17,#0,34,#0,8,#0,6,#0,90,#\n";
    private final String S6_SCRIPT_0 = "0,1,#0,40,#0,6,#0,24,#0,13,#0,5,#0,8,#\n";
    private final String S6_SCRIPT_1 = "0,1,#0,40,#0,6,#0,24,#0,5,#0,5,#0,8,#\n";
    //endregion


    private AppCacheUtil() {
        //需要先初始化SharedPreferencesUtils
        didReadPrivacy = SharedPreferencesUtils.getInstant().didReadPrivacy();
        videoDownloadType = SharedPreferencesUtils.getInstant().getVideoDownloadType();
        handTag = SharedPreferencesUtils.getInstant().getHandTag();

        bgProgram = SharedPreferencesUtils.getInstant().getBgProgram();
        bgLight = SharedPreferencesUtils.getInstant().getBgLight();
        bgPlayer = SharedPreferencesUtils.getInstant().isBgPlayer();
        ignoreSpeedTips = SharedPreferencesUtils.getInstant().isIgnoreSpeedTips();

        initRemoteBeans();
    }

    private static volatile AppCacheUtil instance;

    public static AppCacheUtil getInstance() {
        if (instance == null) {
            synchronized (AppCacheUtil.class) {
                if (instance == null) {
                    instance = new AppCacheUtil();
                }
            }
        }
        return instance;
    }

    public Context getAppContext() {
        return appContext;
    }

    public void setAppContext(Context appContext) {
        this.appContext = appContext;
    }

    public boolean isStoragePermissionEnable() {
        return storagePermissionEnable;
    }

    public void setStoragePermissionEnable(boolean storagePermissionEnable) {
        this.storagePermissionEnable = storagePermissionEnable;
    }

    public boolean isDidReadPrivacy() {
        return didReadPrivacy;
    }

    public void setDidReadPrivacy(boolean didReadPrivacy) {
        this.didReadPrivacy = didReadPrivacy;
        SharedPreferencesUtils.getInstant().setDidReadPrivacy(didReadPrivacy);
    }

    public int getVideoDownloadType() {
        return videoDownloadType;
    }

    public void setVideoDownloadType(int videoDownloadType) {
        this.videoDownloadType = videoDownloadType;
        SharedPreferencesUtils.getInstant().setVideoDownloadType(videoDownloadType);
    }

    public int getHandTag() {
        return handTag;
    }

    public void setHandTag(int handTag) {
        this.handTag = handTag;
        SharedPreferencesUtils.getInstant().setHandTag(handTag);
    }

    public int getBgProgram() {
        return bgProgram;
    }

    public void setBgProgram(int bgProgram) {
        this.bgProgram = bgProgram;
        SharedPreferencesUtils.getInstant().setBgProgram(bgProgram);
    }

    public float getBgLight() {
        return bgLight;
    }

    public void setBgLight(float bgLight) {
        this.bgLight = bgLight;
        SharedPreferencesUtils.getInstant().setBgLight(bgLight);
    }

    public boolean isBgPlayer() {
        return bgPlayer;
    }

    public void setBgPlayer(boolean bgPlayer) {
        this.bgPlayer = bgPlayer;
        SharedPreferencesUtils.getInstant().setBgPlayer(bgPlayer);
    }

    public int getS3Ors5Type() {
        return s3Ors5Type;
    }

    public void setS3Ors5Type(int s3Ors5Type) {
        this.s3Ors5Type = s3Ors5Type;
    }

    public boolean isIgnoreSpeedTips() {
        return ignoreSpeedTips;
    }

    public void setIgnoreSpeedTips(boolean ignoreSpeedTips) {
        this.ignoreSpeedTips = ignoreSpeedTips;
        SharedPreferencesUtils.getInstant().setIgnoreSpeedTips(ignoreSpeedTips);
    }

    //region  代替intent 传值
    private DeviceBean intentBean;
    public void setIntentBean(DeviceBean intentBean) {
        this.intentBean = intentBean;
    }
    public DeviceBean getIntentBean() {
        return intentBean;
    }

    private List<DeviceBean> bindList;
    public List<DeviceBean> getBindList() {
        return bindList;
    }
    public void setBindList(List<DeviceBean> bindList) {
        this.bindList = bindList;
    }
    //endregion

    //region  与遥控器同步的两条脚本

    public int getRemoteModelCount() {
        return remoteModelCount;
    }

    public int getRemoteModelDir() {
        return remoteModelDir;
    }

    public boolean isSynchronizedForRemote() {
        return isSynchronizedForRemote;
    }

    public void setSynchronizedForRemote(boolean synchronizedForRemote) {
        isSynchronizedForRemote = synchronizedForRemote;
    }

    public ModeSettingBean getS6Bean0() {
        return s6Bean0;
    }

    public ModeSettingBean getS6Bean1() {
        return s6Bean1;
    }

    public ModeSettingBean getS8Bean0() {
        return s8Bean0;
    }

    public ModeSettingBean getS8Bean1() {
        return s8Bean1;
    }

    //index 为 0 或者 1
    public String remoteModelName(int index) {
        if (index == 0) {
            return "s0";  //名字简单点， 方便蓝牙传输
        } else {
            return "s1";
        }
    }

//    protocolType 
    public boolean saveRemoteScript(String script, int protocolType, int index) {
        String key = null;
        if (protocolType == ModeConstant.ProtocolTypeS8) {
            if (index == 0) {
                key = S8_SCRIPT_KEY_0;
                s8Bean0.setScript(script);
            } else if (index == 1) {
                key = S8_SCRIPT_KEY_1;
                s8Bean1.setScript(script);
            }
        } else if (protocolType == ModeConstant.ProtocolTypeS6) {
            if (index == 0) {
                key = S6_SCRIPT_KEY_0;
                s6Bean0.setScript(script);
            } else if (index == 1) {
                key = S6_SCRIPT_KEY_1;
                s6Bean1.setScript(script);
            }
        }
        if (key == null) {
            return false;
        }
        SharedPreferencesUtils.getInstant().setStringValue(key, script);
        return true;
    }


    private void initRemoteBeans() {
        s8Bean0 = new ModeSettingBean();
        s8Bean0.setDifficulty(99);
        s8Bean0.setProtocolType(ModeConstant.ProtocolTypeS8);
        s8Bean0.setName_zh(REMOTE_APP_0);
        s8Bean0.setName_en(REMOTE_APP_0);
        String s8Script0 = SharedPreferencesUtils.getInstant().getStringValue(S8_SCRIPT_KEY_0, S8_SCRIPT_0);
        s8Bean0.setScript(s8Script0);

        s8Bean1 = new ModeSettingBean();
        s8Bean1.setDifficulty(99);
        s8Bean1.setProtocolType(ModeConstant.ProtocolTypeS8);
        s8Bean1.setName_zh(REMOTE_APP_1);
        s8Bean1.setName_en(REMOTE_APP_1);
        String s8Script1 = SharedPreferencesUtils.getInstant().getStringValue(S8_SCRIPT_KEY_1, S8_SCRIPT_1);
        s8Bean1.setScript(s8Script1);

        s6Bean0 = new ModeSettingBean();
        s6Bean0.setDifficulty(99);
        s6Bean0.setProtocolType(ModeConstant.ProtocolTypeS6);
        s6Bean0.setName_zh(REMOTE_APP_0);
        s6Bean0.setName_en(REMOTE_APP_0);
        String s6Script0 = SharedPreferencesUtils.getInstant().getStringValue(S6_SCRIPT_KEY_0, S6_SCRIPT_0);
        s6Bean0.setScript(s6Script0);

        s6Bean1 = new ModeSettingBean();
        s6Bean1.setDifficulty(99);
        s6Bean1.setProtocolType(ModeConstant.ProtocolTypeS6);
        s6Bean1.setName_zh(REMOTE_APP_1);
        s6Bean1.setName_en(REMOTE_APP_1);
        String s6Script1 = SharedPreferencesUtils.getInstant().getStringValue(S6_SCRIPT_KEY_1, S6_SCRIPT_1);
        s6Bean1.setScript(s6Script1);
    }

    //endregion
}
